package com.example.myapplication;

import android.animation.Animator;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    Button btn_show;
    Button btnTakePhoto, btnSelect, btnCancel;


    boolean isAnimate = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn_show = findViewById(R.id.btn);
        btn_show.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                btn_show.animate().rotation(90).setDuration(500).setListener(new Animator.AnimatorListener() {
                    @Override
                    public void onAnimationStart(Animator animation) {
                        isAnimate = true;
                    }

                    @Override
                    public void onAnimationEnd(Animator animation) {
                        isAnimate = false;
                    }

                    @Override
                    public void onAnimationCancel(Animator animation) {

                    }

                    @Override
                    public void onAnimationRepeat(Animator animation) {

                    }
                });
                initPopupWindow(view);
            }
        });


    }

    private void initPopupWindow(View v) {
        View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.item_popip, null, false);
        Button btn_xixi = view.findViewById(R.id.btn_xixi);
        Button btn_hehe = view.findViewById(R.id.btn_hehe);
        /**
         * 构造一个PopupWindow，参数依次是加载的View，宽高
         */
        final PopupWindow popupWindow = new PopupWindow(view, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
        //  popupWindow.setAnimationStyle(R.anim.anim_pop);  //设置加载动画
        /**
         * 这些为了点击非PopupWindow区域，PopupWindow会消失的，如果没有下面的
         * 代码的话，你会发现，当你把PopupWindow显示出来了，无论你按多少次后退键
         * PopupWindow并不会关闭，而且退不出程序，加上下述代码可以解决这个问题
         */
        popupWindow.setTouchable(true);
        popupWindow.setTouchInterceptor(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                return false;
                /**
                 *  // 这里如果返回true的话，touch事件将被拦截
                 // 拦截后 PopupWindow的onTouchEvent不被调用，这样点击外部区域无法dismiss
                 */
            }
        });
        popupWindow.setWidth(v.getWidth());
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.alpha = 0.7f;
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        getWindow().setAttributes(lp);
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                WindowManager.LayoutParams lp = getWindow().getAttributes();
                lp.alpha = 1.0f;
                getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
                getWindow().setAttributes(lp);

            }

        });


        /**
         * 要为popWindow设置一个背景才有效
         */
        //   popupWindow.setBackgroundDrawable(new ColorDrawable(0x00000000));
        /**
         * 设置popupWindow显示的位置，参数依次是参照View，x轴的偏移量，y轴的偏移量
         */
        popupWindow.showAsDropDown(v, 0, 0);
        /**
         * 设置popupWindow里的按钮的事件
         */
        btn_xixi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(MainActivity.this, "1111", Toast.LENGTH_SHORT).show();
            }
        });
        btn_hehe.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(MainActivity.this, "2222", Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void initBackPressedPopupWindow() {
        LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
        /**
         * 绑定布局
         */
        View mPopView = inflater.inflate(R.layout.custom_popup_window, null);
        btnTakePhoto = mPopView.findViewById(R.id.id_btn_take_photo);
        btnSelect = mPopView.findViewById(R.id.id_btn_select);
        btnCancel = mPopView.findViewById(R.id.id_btn_cancelo);
        final PopupWindow popupWindow = new PopupWindow(mPopView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
        popupWindow.setContentView(mPopView);// 设置View
        popupWindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);// 设置弹出窗口的宽
        popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);// 设置弹出窗口的高
        popupWindow.setFocusable(true);// 设置弹出窗口可
        popupWindow.setAnimationStyle(R.style.mypopwindow_anim_style);// 设置动画
        popupWindow.setBackgroundDrawable(new ColorDrawable(0x00000000));// 设置背景透明
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.alpha = 0.3f;
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        getWindow().setAttributes(lp);
        mPopView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                int height = view.findViewById(R.id.id_pop_layout).getTop();
                int y = (int) motionEvent.getY();
                if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
                    if (y < height) {
                        popupWindow.dismiss();
                    }
                }
                return true;
            }
        });
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                WindowManager.LayoutParams lp = getWindow().getAttributes();
                lp.alpha = 1.0f;
                getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
                getWindow().setAttributes(lp);
            }

        });
        popupWindow.showAtLocation(MainActivity.this.findViewById(R.id.main), Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
    }

    public void onBackPressed() {
        initBackPressedPopupWindow();
    }
}
